package com.github.tetrisgame.ui.view;

import android.app.GameManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.tetrisgame.manager.BlockManager;
import com.github.tetrisgame.manager.GameCenter;
import com.github.tetrisgame.model.Block;
import com.github.tetrisgame.utils.Constants;

public class GameView extends View {
    private int row = Constants.ROW;
    private int column = Constants.COLUMN;
    private int leftMerge = 0;
    private int topMerge = 0;
    private Bitmap bitmap;
    private Rect rect = new Rect();
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    public GameView(Context context) {
        this(context,null);
    }

    public GameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mWidth = Constants.SIZE * Constants.COLUMN;
        int mHeight = Constants.SIZE * Constants.ROW;
        int realWidth;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY){
            realWidth = widthSize;
        }else {
            realWidth = mWidth;
        }
        int realHeight;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY){
            realHeight = heightSize;
        }else {
            realHeight = mHeight;
        }
        row = realHeight / Constants.SIZE;
        column = realWidth / Constants.SIZE;
        GameCenter.defaultCenter.setBoardSize(row,column);
        leftMerge = (realWidth - column*Constants.SIZE)/2;
        topMerge = (realHeight - row*Constants.SIZE)/2;
        setMeasuredDimension(realWidth,realHeight);

    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        drawBackgroundGrid(canvas);
        drawCurrentBlock(canvas);
        drawFixedBlock(canvas);
    }

    private void drawFixedBlock(Canvas canvas) {
        int[][] boards = GameCenter.defaultCenter.getBoards();
        for (int i = 0; i < boards.length; i++) {
            for (int j = 0; j < boards[0].length; j++) {
                if(boards[i][j] != 0){
                    rect.left = j*Constants.SIZE + leftMerge;
                    rect.top = i*Constants.SIZE + topMerge;
                    rect.right = (j+1)*Constants.SIZE + leftMerge;
                    rect.bottom = (i+1)*Constants.SIZE + topMerge;

                    canvas.drawBitmap(getFixedBlockBimtap(boards[i][j]),null,rect,null);
                }
            }
        }
    }

    private Bitmap getFixedBlockBimtap(int i) {
        switch (i){
            case 1 : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(1));
            case 2 : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(2));
            case 3 : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(3));
            case 4 : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(4));
            case 5 : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(5));
            default : return BitmapFactory.decodeResource(getResources(),Constants.getFixedImageRes(5));
        }

    }

    private void drawBackgroundGrid(Canvas canvas){
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if((i+j)%2 == 0){
                    mPaint.setColor(Color.parseColor("#ADD8E6"));
                }else{
                    mPaint.setColor(Color.parseColor("#FFF3E0"));
                }
                canvas.drawRect(j*Constants.SIZE+leftMerge,i*Constants.SIZE+topMerge,(j+1)*Constants.SIZE+leftMerge,(i+1)*Constants.SIZE+topMerge,mPaint);
            }
        }
    }
    private void drawCurrentBlock(Canvas canvas){
        Block block = BlockManager.defaultManager.getCurrentBlock();
        int[][] shapes = block.getShape();
        bitmap = BitmapFactory.decodeResource(getResources(),block.getImageResource());
        for (int i = 0; i < shapes.length; i++) {
            for (int j = 0; j < shapes[0].length; j++) {
                if(shapes[i][j] != 0){
                    rect.left = (j + block.getColumn())*Constants.SIZE+leftMerge;
                    rect.top = (i + block.getRow())*Constants.SIZE+topMerge;
                    rect.right = (j + 1 + block.getColumn())*Constants.SIZE+leftMerge;
                    rect.bottom = (i + 1 + block.getRow())*Constants.SIZE+topMerge;
                    canvas.drawBitmap(bitmap,null,rect,null);
                }
            }
        }
    }

    public void refresh() {
        invalidate();
    }
}
